/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.jobs.service;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.javaeye.accounting.dao.MaterialsMonthBatchDetailDAO;
import com.javaeye.accounting.dto.MaterialsMonthBatchDetail;
import com.javaeye.common.util.DateUtils;
import com.javaeye.store.dao.MaterialsBatchDetailDAO;
import com.javaeye.store.dao.MaterialsChangeDetailDAO;
import com.javaeye.store.dto.MaterialsBatchDetail;
import com.javaeye.store.dto.MaterialsChangeDetail;
import com.javaeye.store.dto.MaterialsSKUDetail;

public class MonthReportJobService implements IMonthReportJobService {
	
	private static Log log = LogFactory.getLog(TestJobBean.class);
	
	private MaterialsChangeDetailDAO materialsChangeDetailDao;
	
	private MaterialsMonthBatchDetailDAO materialsMonthBatchDetailDao;
	
	private MaterialsBatchDetailDAO materialsBatchDetailDao;
	
	public void setMaterialsChangeDetailDao(
			MaterialsChangeDetailDAO materialsChangeDetailDao) {
		this.materialsChangeDetailDao = materialsChangeDetailDao;
	}

	public void setMaterialsMonthBatchDetailDao(
			MaterialsMonthBatchDetailDAO materialsMonthBatchDetailDao) {
		this.materialsMonthBatchDetailDao = materialsMonthBatchDetailDao;
	}

	public void setMaterialsBatchDetailDao(
			MaterialsBatchDetailDAO materialsBatchDetailDao) {
		this.materialsBatchDetailDao = materialsBatchDetailDao;
	}

	public void createMonthReport() {
		log.info(DateUtils.dateToString(DateUtils.PATTEN_HMS) + "开始库存月报作业");
		//判断本期的库存月报是否已经生成
		String currentMonthNo = DateUtils.getCurrentMonthNo();
		long monthInfoCount = materialsMonthBatchDetailDao.getMonthInfoCount(currentMonthNo);
		if (monthInfoCount > 0) {
			log.info("本期(" + currentMonthNo + ")的库存月报已经生成,退出作业");
			return;
		}
		
		String beforeMonthNo = DateUtils.getBeforeMonthNo();
		int count = 0;
		
		Date beginDate = DateUtils.getBeforeMonthBeginDate();
		Date endDate = DateUtils.getBeforeMonthEndDate();
		
		Set<Integer> materialsIds = new HashSet<Integer>();
		Set<String> batchNos = new HashSet<String>();
		
		List<Integer> beforeMaterialsIds = materialsMonthBatchDetailDao.getMaterialsIds(beforeMonthNo);
		materialsIds.addAll(beforeMaterialsIds);
		List<Integer> currentMaterialsIds = materialsChangeDetailDao.getMaterialsIds(beginDate, endDate);
		materialsIds.addAll(currentMaterialsIds);
		
		for (Integer materialsId : materialsIds) {
			batchNos.clear();
			List<String> beforeBatchNos = materialsMonthBatchDetailDao.getBatchNos(materialsId, beforeMonthNo);
			batchNos.addAll(beforeBatchNos);
			List<String> currentBatchNos = materialsChangeDetailDao.getBatchNos(beginDate, endDate, materialsId);
			batchNos.addAll(currentBatchNos);
			for (String batchNo : batchNos) {
				MaterialsBatchDetail batchDetail = 
					materialsBatchDetailDao.getBatchDetail(materialsId, batchNo);
				//取得上期库存数量
				MaterialsMonthBatchDetail monthBatchDetail = 
					materialsMonthBatchDetailDao.getMaterialsMonthBatchDetail(materialsId, batchNo, beforeMonthNo);
				float number = 0;
				float defectiveNumber = 0;
				double price = batchDetail.getPrice();
				if (monthBatchDetail != null) {
					number = monthBatchDetail.getNumber();
					defectiveNumber = monthBatchDetail.getDefectiveNumber();
				}
				
				//计算本期库存数量
				List<MaterialsChangeDetail> changeDetails = 
					materialsChangeDetailDao.getMaterialsChangeInfo(beginDate, endDate, materialsId, batchNo);
				for (MaterialsChangeDetail changeDetail : changeDetails) {
					
					int changeType = changeDetail.getChangeType();
					switch (changeType) {
					case MaterialsChangeDetail.PLAN_INSTORE:
					case MaterialsChangeDetail.PROCUREMENT_QUALIFIED_INSTORE:
					case MaterialsChangeDetail.MATERIALS_QUALIFIED_INSTORE:
					case MaterialsChangeDetail.ADJUST_INVENTORY_QUALIFIED_IN_STORE:
						number += changeDetail.getNumber();
						break;
					case MaterialsChangeDetail.PROCUREMENT_UNQUALIFIED_INSTORE:
					case MaterialsChangeDetail.MATERIALS_UNQUALIFIED_INSTORE:
					case MaterialsChangeDetail.ADJUST_INVENTORY_UNQUALIFIED_IN_STORE:
						defectiveNumber += changeDetail.getNumber();
						break;
					case MaterialsChangeDetail.PRODUCT_OUTSTORE:
					case MaterialsChangeDetail.MATERIALS_OUTSTORE:
					case MaterialsChangeDetail.ADJUST_INVENTORY_QUALIFIED_OUT_STORE:
						number -= changeDetail.getNumber();
						break;
					case MaterialsChangeDetail.PROCUREMENT_UNQUALIFIED_OUTSTORE:
					case MaterialsChangeDetail.ADJUST_INVENTORY_UNQUALIFIED_OUT_STORE:
						defectiveNumber -= changeDetail.getNumber();
						break;
					default:
						break;
					}
				}
				//保存
				MaterialsMonthBatchDetail currentMonthBatchDetail = new MaterialsMonthBatchDetail();
				MaterialsSKUDetail skuDetail = new MaterialsSKUDetail();
				skuDetail.setId(materialsId);
				currentMonthBatchDetail.setSkuDetail(skuDetail);
				currentMonthBatchDetail.setBatchNo(batchNo);
				currentMonthBatchDetail.setMonthNo(currentMonthNo);
				currentMonthBatchDetail.setNumber(number);
				currentMonthBatchDetail.setDefectiveNumber(defectiveNumber);
				currentMonthBatchDetail.setPrice(price);
				materialsMonthBatchDetailDao.saveMaterialsMonthBatchDetail(currentMonthBatchDetail);
				count++;
			}
		}
		log.info("本期(" + currentMonthNo + ")的库存月报生成成功，作业共计作成记录(" + count + ")条");
		log.info(DateUtils.dateToString(DateUtils.PATTEN_HMS) + "结束库存月报作业");
	}

}
